<template>
  <div style="padding:15px">
    <vxe-toolbar id="chargingTypeId" :setting="{storage: true}" export :refresh="{query: findList}">
      <template v-slot:buttons>
        <el-button @click="findList">查询</el-button>
        <el-button v-if="branch.level <1" @click="funcAdd">新增</el-button>
      </template>
    </vxe-toolbar>
    <vxe-table
      ref="xTable"
      border
      resizable
      stripe
      highlight-current-row
      show-footer
      highlight-hover-row
      row-id="id"
      class="mytable-footer"
      height="700"

      :data="tableData"
      remote-sort
      @sort-change="sortList"
    >
      <vxe-table-column>
        <vxe-table-column field="shipmentName" title="客户名称" min-width="100" sortable />
        <template v-slot:header="{ column }">
          <el-input v-model="listQuery.shipmentName" placeholder="请输入客户名称" clearable />
        </template>
      </vxe-table-column>
      <vxe-table-column>
        <vxe-table-column field="chargingType" title="计费类型" min-width="100" sortable />
        <template v-slot:header="{ column }">

          <costTypeSelect ref="costType" @queryCostType="selectCostType" />
        </template>
      </vxe-table-column>
      <vxe-table-column>
        <vxe-table-column field="price" title="单价" min-width="100" sortable />
        <template v-slot:header="{ column }">
          <el-input v-model="listQuery.price" placeholder="请输入单价" clearable />
        </template>
      </vxe-table-column>
      <vxe-table-column>
        <vxe-table-column field="isDefault" title="默认" min-width="100" sortable  :formatter="formattertype" />
        <template v-slot:header="{ column }">
          <el-select v-model="listQuery.isDefault" placeholder="请选择默认" clearable>
            <el-option value="0" label="非默认"/>
            <el-option value="1" label="默认"/>
          </el-select>
        </template>
      </vxe-table-column>
      <vxe-table-column>
        <vxe-table-column
          field="chargingMode"
          title="计费方式"
          :formatter="formatterChargingMode"
          min-width="100"
          sortable
        />
        <template v-slot:header="{ column }">
          <el-select v-model="listQuery.chargingMode" placeholder="请选择" clearable>
            <el-option value="" label="请选择" />
            <el-option value="1" label="吨" />
          </el-select>
        </template>
      </vxe-table-column>
    <!--  <vxe-table-column>
        <vxe-table-column field="startDate" title="有效期" min-width="350" :formatter="termOfValidityFormatter" />
        <template v-slot:header="{ column }">
          <el-date-picker
            v-model="listQuery.validityDate"
            type="daterange"
            style="width:100%"
            range-separator="至"

            value-format="yyyy-MM-dd"
            start-placeholder="开始日期"
            :picker-options="pickerOptions"
            end-placeholder="结束日期"
            @change="changeDate"
          />
        </template>
      </vxe-table-column>-->
      <vxe-table-column v-if="branch.level <1" title="操作" fixed="right" width="180">
        <template slot-scope="scope">
          <el-tooltip v-if="scope.row.isDefault == 0"  class="item" effect="dark" content="默认" placement="top">
            <vxe-button type="text" @click="funcIsDefault(scope.row.code)">
              <i class="el-icon-circle-check" />
            </vxe-button>
          </el-tooltip>
          <el-tooltip  class="item" effect="dark" content="编辑" placement="top">
            <vxe-button type="text" @click="funcEdit(scope.row.code)">
              <i class="el-icon-edit" />
            </vxe-button>
          </el-tooltip>
          <el-tooltip class="item" effect="dark" content="删除" placement="top">
            <vxe-button type="text" @click="funcDelete(scope.row.code)">
              <i class="el-icon-delete" />
            </vxe-button>
          </el-tooltip>
        </template>
      </vxe-table-column>
    </vxe-table>
    <vxe-pager
      :loading="loading"
      :current-page.sync="listQuery.pageNum"
      :page-size.sync="listQuery.pageSize"
      :total="listQuery.total"
      :layouts="['PrevJump', 'PrevPage', 'Number', 'NextPage', 'NextJump', 'Sizes', 'FullJump', 'Total']"
      @page-change="handlePageChange"
    />

    <vxe-modal v-model="modelAddFlag" title="新增仓储计费协议" width="500" height="570" resize>
      <compAdd ref="addRef" @fatherAddMethod="fatherAdd" @fatherCancelMethod="fatherCancel" />
    </vxe-modal>
    <vxe-modal v-model="modelEditFlag" title="编辑仓储计费协议" width="500" height="570" resize>
      <compEdit ref="editRef" @fatherEditMethod="fatherEdit" @fatherCancelMethod="fatherCancel" />
    </vxe-modal>


  </div>
</template>
<script>
import Vue from 'vue'
import 'xe-utils'
import VXETable from 'vxe-table'
import 'vxe-table/lib/index.css'
import XEUtils from 'xe-utils'
import VXEUtils from 'vxe-utils'
import compAdd from './components/chargingTypeAdd.vue'
import compEdit from './components/chargingTypeEdit.vue'
import compDetail from './components/chargingTypeDetail.vue'
import costTypeSelect from './components/costTypeSelect.vue'
import request from '@/utils/request'

Vue.use(VXETable, VXEUtils, XEUtils, { mounts: ['cookie'] })

export default {
  name: 'ChargingTypeList',
  components: {
    compAdd,
    compEdit,
    compDetail,
    costTypeSelect
  },
  data() {
    return {
      //时间区间
     /* pickerOptions: {
        shortcuts: [
          {
            text: '最近一周',
            onClick(picker) {
              const end = new Date()
              const start = new Date()
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 7)
              picker.$emit('pick', [start, end])
            }
          },
          {
            text: '最近一个月',
            onClick(picker) {
              const end = new Date()
              const start = new Date()
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 30)
              picker.$emit('pick', [start, end])
            }
          },
          {
            text: '最近三个月',
            onClick(picker) {
              const end = new Date()
              const start = new Date()
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 90)
              picker.$emit('pick', [start, end])
            }
          }
        ]
      },*/


      message: '',
      loading: false,
      tableData: [],
      cascadeData: [],
      modelAddFlag: false,
      modelEditFlag: false,
      modelDetailFlag: false,
      copy: false,
      partyGroup:[],

      listQuery: {
        shipmentName: '',
        partyGroupCode:'',

        costType: '',
        chargingType: '',
        price: '',
        chargingMode: '',
       /* validityDate: '',
        endDate: '',*/
        pageNum: 1,
        pageSize: 10,
        sortField: '',
        sortType: '',
        total: 0
      },
      branch: this.$store.getters.branch
    }
  },
  created() {
    // this.getCascadeData()
    this.getPartyGroup()
  },
  methods: {
    // 翻译破损类型
    formattertype({ cellValue }) {
      if (cellValue == 0){
        return "非默认"
      } else if (cellValue == 1){
        return "默认"
      }
    },
    //获取机构信息
    getPartyGroup() {
      request({
        url: '/arpa-basic-api/partyGroup/getPartyGroup',
        method: 'get'
      }).then(response => {
        this.partyGroup = response.data
      }).then(response => {
        this.getCascadeData()
      })
    },

    // 选择托盘状态
    selectCostType(value) {
      this.listQuery.chargingType = value[1]
    },
   /* // 有效期格式化
    showCostType({ row }) {
      return row.startDate + ' 至 ' + row.endDate
    }, // 有效期格式化
    termOfValidityFormatter({ row }) {
      return row.startDate + ' 至 ' + row.endDate
    },*/

    // 获取字典数据
    getCascadeData() {
      request({
        url: '/arpa-basic-api/costType/cascadeData',
        method: 'get'
      }).then(response => {
        this.cascadeData = response.data

        this.$refs.costType.initData(this.cascadeData, 'queryCostType')
      }).then(response => {
        this.findList()
      })
    },
    // 翻译类型
    handleChange(value) {
    },
    formatterChargingMode({ cellValue }) {
      if (cellValue == '1') {
        return '吨'
      }
    },
    /*changeDate(value) {

      if(value){
        this.listQuery.startDate = value[0]
        this.listQuery.endDate = value[1]
      }else{
        this.listQuery.startDate = ''
        this.listQuery.endDate = ''
      }
    },*/

    findList() {
      return new Promise(resolve => {
        request({
          url: '/arpa-basic-api/chargingType',
          method: 'get',
          params: this.listQuery
        }).then(response => {
          this.tableData = response.data.records
          this.listQuery.total = response.data.total
          resolve();
        })
      })
    },
    // 排序
    sortList({ column, property, order }) {
      if (order) {
        this.listQuery.sortField = property
        this.listQuery.sortType = order
      } else {
        this.listQuery.sortField = ''
      }
      this.findList()
    },
    // 添加方法
    funcAdd() {
      this.$refs.addRef.reset();    //重置

      this.$refs.addRef.$refs.costType.initData(this.cascadeData, 'queryCostType')
      this.$refs.addRef.partyGroup = this.partyGroup

      this.modelAddFlag = true
    },
    // 默认
    funcIsDefault(code) {
      request({ url: `/arpa-basic-api/chargingType/shipmentCode/${code}`, method: 'get' }).then(response => {
        this.$message({ message: response.msg, type: 'success' })
        this.findList()
      })
    },
    // 修改方法
    funcEdit(code) {
      this.$refs.editRef.$refs.costType.initData(this.cascadeData, 'queryCostType')
      this.$refs.editRef.partyGroup = this.partyGroup

      this.$refs.editRef.getInfo(code)
      this.modelEditFlag = true
    },
    // 详情方法
    funcDetail(code) {
      this.$refs.detailRef.getInfo(code)
      this.modelDetailFlag = true
    },
    // 删除方法
    funcDelete(row) {
      this.$confirm('确认删除吗？').then(_ => {
        request({
          url: `/arpa-basic-api/chargingType`,
          method: 'delete',
          params: { codes: row }
        }).then(response => {
          this.$message({ message: response.msg, type: 'success' })
          this.findList()
        })
      }).catch(_ => {
      })
    },
    // 增加组件的父方法
    fatherAdd() {
      this.modelAddFlag = false
      this.findList()
    },
    // 编辑组件的父方法
    fatherEdit() {
      this.modelEditFlag = false
      this.findList()
    },
    // 公共取消
    fatherCancel(modelFlag) {
      switch (modelFlag) {
        case 'add':
          this.modelAddFlag = false
          break
        case 'edit':
          this.modelEditFlag = false
          break
        case 'detail':
          this.modelDetailFlag = false
          break
        default:
      }
    },
    // 分页handle
    handlePageChange({ currentPage, pageSize }) {
      this.listQuery.pageNum = currentPage
      this.listQuery.pageSize = pageSize
      this.findList()
    },/*
    // 底部颜色设置
    footerCellClassName({ $rowIndex, column, columnIndex }) {
      if (columnIndex === 0) {
        if ($rowIndex === 0) {
          return 'col-blue'
        } else {
          return 'col-red'
        }
      }
    }, */
    // 设置底部合计
    footerMethod({ columns, data }) {
      return [
        columns.map((column, columnIndex) => {
          if (columnIndex === 0) {
            return '合计'
          }
          if (['pageviews', 'forecast'].includes(column.property)) {
            return XEUtils.sum(data, column.property)
          }
          return null
        })
      ]
    }
  }
}
</script>

<style>
.vxe-table .vxe-body--column:not(.col--ellipsis), .vxe-table .vxe-footer--column:not(.col--ellipsis), .vxe-table .vxe-header--column:not(.col--ellipsis) {
 padding: 6px 0;
}
.vxe-toolbar{padding:0 0 10px 0}
</style>
